package com.cloud.translation.TTS;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.widget.Toast;

import java.util.Locale;

public class MyTTS implements TextToSpeech.OnInitListener {
    private TextToSpeech textToSpeech;
    private Context context;
    private String string;

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = textToSpeech.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(context, "Data loss or unsupported", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public MyTTS(Context context){
        this.context=context;
        this.textToSpeech=new TextToSpeech(context,this);
    }

    public MyTTS(Context context,String text){
        this.context=context;
        string=text;
        this.textToSpeech=new TextToSpeech(context,this);
    }

    public void mySpeak(String text){
        textToSpeech.setPitch(1.0f);
        textToSpeech.setSpeechRate(1.0f);
        textToSpeech.speak(text,TextToSpeech.QUEUE_ADD,null);
    }

    public void stop(){
        textToSpeech.stop();
        textToSpeech.shutdown();
        textToSpeech=null;
    }

}
